clear 
use "C:\Users\Sunyoung\Desktop\WTF & Polity Research Note\Data and Replication Files\Willingness to Fight and Polity Research Note Dataset.dta"

pwcorr Fight Polity2_lag , sig star(.001)

///
global country "Polity2_lag"    
global individual "Male Married Age Age_sq Income Education_level" 
global opt ""      
asdoc meqrlogit Fight $country $opt $individual if touse==1, || ccodecow:, nested save(Table I - Polity and individual willingness to fight) replace setstars(***@.001, **@.01, *@.05) tzok dec(3) cnames(1) title(Table I. Polity and individual willingness to fight)

global country "lngdppc_lag Polity2_lag"    
global individual "Male Married Age Age_sq Income Education_level" 
global opt ""      
asdoc meqrlogit Fight $country $opt $individual if touse==1, || ccodecow:, nested save(Table I - Polity and individual willingness to fight) append setstars(***@.001, **@.01, *@.05) tzok dec(3) cnames(1) title(Table I. Polity and individual willingness to fight)

global country "lngdppc_lag Polity2_lag"    
global individual "Male Married Age Age_sq Income Education_level" 
global opt "i.WVS_wave i.Outlier"      
asdoc meqrlogit Fight $country $opt $individual if touse==1, || ccodecow:, nested save(Table I - Polity and individual willingness to fight) append setstars(***@.001, **@.01, *@.05) tzok dec(3) cnames(1) title(Table I. Polity and individual willingness to fight)

global country "lngdppc_lag Polity2_lag"    
global individual "Male Married Age Age_sq Income Education_level National_pride_dummy" 
global opt "i.WVS_wave i.Outlier"      
asdoc meqrlogit Fight $country $opt $individual if touse==1, || ccodecow:, nested save(Table I - Polity and individual willingness to fight) append setstars(***@.001, **@.01, *@.05) tzok dec(3) cnames(1) title(Table I. Polity and individual willingness to fight)

global country "lngdppc_lag Polity2_lag Revisionist"    
global individual "Male Married Age Age_sq Income Education_level" 
global opt "i.WVS_wave i.Outlier"      
asdoc meqrlogit Fight $country $opt $individual if touse==1, || ccodecow:, nested save(Table I - Polity and individual willingness to fight) append setstars(***@.001, **@.01, *@.05) tzok dec(3) cnames(1) title(Table I. Polity and individual willingness to fight)

global country "lngdppc_lag Polity2_lag Revisionist"    
global individual "Male Married Age Age_sq Income Education_level National_pride_dummy" 
global opt "i.WVS_wave i.Outlier"      
asdoc meqrlogit Fight $country $opt $individual if touse==1, || ccodecow:, nested save(Table I - Polity and individual willingness to fight) append setstars(***@.001, **@.01, *@.05) tzok dec(3) cnames(1) title(Table I. Polity and individual willingness to fight)

*Correlation matrix reveals none of the variables in the model are correlated.
correlate lngdppc_lag Polity2_lag Male Married Age Age_sq Income Education_level 
pwcorr lngdppc_lag Polity2_lag Male Married Age Age_sq Income Education_level 

*Variance Inflation Indicator
regress Fight lngdppc_lag Polity2_lag Revisionist Male Married Age Age_sq Income Education_level National_pride_dummy i.WVS_wave i.Outlier      

estat vif

///

global country "lngdppc_lag_bar lngdppc_lag_within Polity2_lag_bar Polity2_lag_within"    
global individual "Male Married Age Age_sq Income Education_level" 
global opt ""      
asdoc meqrlogit Fight $country $opt $individual if touse==1, || ccodecow:, nested save(Table II - Mundlak decomposition - Polity and individual willingness to fight) replace setstars(***@.001, **@.01, *@.05) tzok dec(3) cnames(1) title(Table II. Mundlak decomposition - Polity and individual willingness to fight)


test lngdppc_lag_bar Polity2_lag_bar

global country "lngdppc_lag_bar lngdppc_lag_within Polity2_lag_bar Polity2_lag_within"    
global individual "Male Married Age Age_sq Income Education_level" 
global opt "i.WVS_wave i.Outlier"      
asdoc meqrlogit Fight $country $opt $individual if touse==1, || ccodecow:, nested save(Table II - Mundlak decomposition - Polity and individual willingness to fight) append setstars(***@.001, **@.01, *@.05) tzok dec(3) cnames(1) title(Table II. Mundlak decomposition - Polity and individual willingness to fight)

estimates save Mundlak_results_for_Wald_Test, replace 

margins, at(Polity2_lag_within = (-5(2)5)) atmeans predict(mu fixedonly)
marginsplot, title("Predicted Pr(Willingness to Fight) by Within-Country Polity") ytitle("Predicted probability") xtitle("Within-country Polity (deviation)")

margins, at(Polity2_lag_bar = (-10(2)10)) atmeans predict(mu fixedonly)
marginsplot, title("Predicted Pr(Willingness to Fight) by Between-Country Polity") ytitle("Predicted probability") xtitle("Country mean Polity")

predict reffects, reffects
twoway (scatter reffects ccodecow, mlabel(ccodecow)), title("Country-Level Random Intercepts: Baseline Willingness to Fight") ytitle("Random Intercept (u_j)") xtitle("Country Code")

_pctile lngdppc_lag_bar, p(25 75)
local g25 = r(r1)
local g75 = r(r2)
margins, at(Polity2_lag_bar=(-10(5)10) lngdppc_lag_bar=(`g25' `g75')) predict(mu fixedonly)

*margins, at(lngdppc_lag_bar=(7(1)10) Polity2_lag_bar=(-10(5)10))
*marginsplot, title("Predicted Willingness to Fight across Democracy and Development") ytitle("Predicted Probability") xtitle("Democracy (Polity2)") plotdim(lngdppc_lag_bar)

global country "lngdppc_lag_bar lngdppc_lag_within Polity2_lag_bar Polity2_lag_within"    
global individual "Male Married Age Age_sq Income Education_level National_pride_dummy" 
global opt "i.WVS_wave i.Outlier"      
asdoc meqrlogit Fight $country $opt $individual if touse==1, || ccodecow:, nested save(Table II - Mundlak decomposition - Polity and individual willingness to fight) append setstars(***@.001, **@.01, *@.05) tzok dec(3) cnames(1) title(Table II. Mundlak decomposition - Polity and individual willingness to fight)

global country "lngdppc_lag_bar lngdppc_lag_within Polity2_lag_bar Polity2_lag_within Revisionist_bar Revisionist_within"    
global individual "Male Married Age Age_sq Income Education_level" 
global opt "i.WVS_wave i.Outlier"      
asdoc meqrlogit Fight $country $opt $individual if touse==1, || ccodecow:, nested save(Table II - Mundlak decomposition - Polity and individual willingness to fight) append setstars(***@.001, **@.01, *@.05) tzok dec(3) cnames(1) title(Table II. Mundlak decomposition - Polity and individual willingness to fight)

estimates save Mundlak_results_for_Wald_Test_Revisionist, replace 
test lngdppc_lag_bar Polity2_lag_bar Revisionist_bar

global country "lngdppc_lag_bar lngdppc_lag_within Polity2_lag_bar Polity2_lag_within Revisionist_bar Revisionist_within"    
global individual "Male Married Age Age_sq Income Education_level National_pride_dummy" 
global opt "i.WVS_wave i.Outlier"      
asdoc meqrlogit Fight $country $opt $individual if touse==1, || ccodecow:, nested save(Table II - Mundlak decomposition - Polity and individual willingness to fight) append setstars(***@.001, **@.01, *@.05) tzok dec(3) cnames(1) title(Table II. Mundlak decomposition - Polity and individual willingness to fight)

estimates store M1

coefplot M1, keep(*_bar *_within) coeflabels(Fight:lngdppc_lag_bar = "GDP (Between)" Fight:lngdppc_lag_within   = "GDP (Within)" Fight:Polity2_lag_bar = "Democracy (Between)" Fight:Polity2_lag_within = "Democracy (Within)" Fight:Revisionist_bar = "Revisionist (Between)" Fight:Revisionist_within   = "Revisionist (Within)") xline(0) scheme(s1color) title("Within vs Between Country Effects on Willingness to Fight", size(medium)) xlabel(-.5(.1).5)


////No Decomposed Baseline Willingness to Fight Graphs

bys ccodecow: egen polity_bar = mean(Polity2_lag_bar)
gen Democracy = polity_bar >= 6 if !missing(polity_bar)
gen Autocracy = polity_bar <= -6 if !missing(polity_bar)
gen Anocracy = polity_bar < 6 & polity_bar > -6 if !missing(polity_bar)
egen tag = tag(ccodecow)

global country "lngdppc_lag Polity2_lag"    
global individual "Male Married Age Age_sq Income Education_level" 
global opt "i.WVS_wave i.Outlier"      
meqrlogit Fight $country $opt $individual if touse==1, || ccodecow:, 

predict u_j, reffects
predict se_u_j, reses
gen lo = u_j - 1.96*se_u_j
gen hi = u_j + 1.96*se_u_j

/// Democracies
count if Democracy==1 & tag
local N = r(N)
local ymin = 0.5
local ymax = `N' + 0.5

egen order_demo = rank(u_j) if Democracy==1 & tag, unique

twoway (rcap lo hi order_demo if Democracy==1 & tag, horizontal lcolor(gs8) lwidth(medthin)) (scatter order_demo u_j if Democracy==1 & tag, msymbol(o) mcolor(gs8) mlabel(COW_NUM) mlabcolor(black) mlabsize(vsmall) mlabpos(3) mlabgap(1)), yscale(reverse range(`ymin' `ymax') nofextend) ylabel(none) ytitle("") xline(0, lpattern(dash) lcolor(gs8)) xtitle("Country random intercept, u_j (democracies)") title("Democracies") plotregion(margin(zero)) graphregion(margin(small)) legend(off)

graph save "No Decompostion - Baseline - Willingness to Fight - Democracies.gph", replace

/// Autocracies
count if Autocracy==1 & tag
local N = r(N)
local ymin = 0.5
local ymax = `N' + 0.5

egen order_auto = rank(u_j) if Autocracy==1 & tag, unique

twoway (rcap lo hi order_auto if Autocracy==1 & tag, horizontal lcolor(gs8) lwidth(medthin)) (scatter order_auto u_j if Autocracy==1 & tag, msymbol(o) mcolor(gs8) mlabel(COW_NUM) mlabcolor(black) mlabsize(vsmall) mlabpos(3) mlabgap(1)), yscale(reverse range(`ymin' `ymax') nofextend) ylabel(none) ytitle("") xline(0, lpattern(dash) lcolor(gs8)) xtitle("Country random intercept, u_j (autocracies)") title("Autocracies") plotregion(margin(zero)) graphregion(margin(small)) legend(off)

graph save "No Decompostion - Baseline - Willingness to Fight - Autocracies.gph", replace

/// Anocracies
count if Anocracy==1 & tag
local N = r(N)
local ymin = 0.5
local ymax = `N' + 0.5

egen order_anoc = rank(u_j) if Anocracy==1 & tag, unique

twoway (rcap lo hi order_anoc if Anocracy==1 & tag, horizontal lcolor(gs8) lwidth(medthin)) (scatter order_anoc u_j if Anocracy==1 & tag, msymbol(o) mcolor(gs8) mlabel(COW_NUM) mlabcolor(black) mlabsize(vsmall) mlabpos(3) mlabgap(1)), yscale(reverse range(`ymin' `ymax') nofextend) ylabel(none) ytitle("") xline(0, lpattern(dash) lcolor(gs8)) xtitle("Country random intercept, u_j (anocracies)") title("Anocracies") plotregion(margin(zero)) graphregion(margin(small)) legend(off)

graph save "No Decompostion - Baseline - Willingness to Fight - Anocracies.gph", replace


/// Three above graphs merged into one

graph use "No Decompostion - Baseline - Willingness to Fight - Autocracies.gph", name(g_auto, replace)
graph use "No Decompostion - Baseline - Willingness to Fight - Anocracies.gph", name(g_ano, replace)
graph use "No Decompostion - Baseline - Willingness to Fight - Democracies.gph", name(g_demo, replace)
graph combine g_auto g_ano g_demo, cols(3) title("Figure 1: No decomposition baseline willingness to fight by regime type")imargin(small) graphregion(color(white)) note("")

graph save "Figure 1 - No Decomposition Baseline - Willingness to Fight - Regime Types.gph", replace
graph export "Figure 1 - No Decomposition Baseline - Willingness to Fight - Regime Types.png", width(2400) replace


/////Decomposed Baseline Willingness to Fight Graphs

drop u_j se_u_j lo hi order_demo order_auto order_anoc

bys ccodecow: egen polity_bar = mean(Polity2_lag_bar)
gen Democracy = polity_bar >= 6 if !missing(polity_bar)
gen Autocracy = polity_bar <= -6 if !missing(polity_bar)
gen Anocracy = polity_bar < 6 & polity_bar > -6 if !missing(polity_bar)
egen tag = tag(ccodecow)

global country "lngdppc_lag_bar lngdppc_lag_within Polity2_lag_bar Polity2_lag_within"    
global individual "Male Married Age Age_sq Income Education_level" 
global opt "i.WVS_wave i.Outlier"      
meqrlogit Fight $country $opt $individual if touse==1, || ccodecow:, 

predict u_j, reffects
predict se_u_j, reses
gen lo = u_j - 1.96*se_u_j
gen hi = u_j + 1.96*se_u_j

/// Democracies
count if Democracy==1 & tag
local N = r(N)
local ymin = 0.5
local ymax = `N' + 0.5

egen order_demo = rank(u_j) if Democracy==1 & tag, unique

twoway (rcap lo hi order_demo if Democracy==1 & tag, horizontal lcolor(gs8) lwidth(medthin)) (scatter order_demo u_j if Democracy==1 & tag, msymbol(o) mcolor(gs8) mlabel(COW_NUM) mlabcolor(black) mlabsize(vsmall) mlabpos(3) mlabgap(1)), yscale(reverse range(`ymin' `ymax') nofextend) ylabel(none) ytitle("") xline(0, lpattern(dash) lcolor(gs8)) xtitle("Country random intercept, u_j (democracies)") title("Democracies") plotregion(margin(zero)) graphregion(margin(small)) legend(off)

graph save "Baseline - Willingness to Fight - Democracies.gph", replace

/// Autocracies
count if Autocracy==1 & tag
local N = r(N)
local ymin = 0.5
local ymax = `N' + 0.5

egen order_auto = rank(u_j) if Autocracy==1 & tag, unique

twoway (rcap lo hi order_auto if Autocracy==1 & tag, horizontal lcolor(gs8) lwidth(medthin)) (scatter order_auto u_j if Autocracy==1 & tag, msymbol(o) mcolor(gs8) mlabel(COW_NUM) mlabcolor(black) mlabsize(vsmall) mlabpos(3) mlabgap(1)), yscale(reverse range(`ymin' `ymax') nofextend) ylabel(none) ytitle("") xline(0, lpattern(dash) lcolor(gs8)) xtitle("Country random intercept, u_j (autocracies)") title("Autocracies") plotregion(margin(zero)) graphregion(margin(small)) legend(off)

graph save "Baseline - Willingness to Fight - Autocracies.gph", replace

/// Anocracies
count if Anocracy==1 & tag
local N = r(N)
local ymin = 0.5
local ymax = `N' + 0.5

egen order_anoc = rank(u_j) if Anocracy==1 & tag, unique

twoway (rcap lo hi order_anoc if Anocracy==1 & tag, horizontal lcolor(gs8) lwidth(medthin)) (scatter order_anoc u_j if Anocracy==1 & tag, msymbol(o) mcolor(gs8) mlabel(COW_NUM) mlabcolor(black) mlabsize(vsmall) mlabpos(3) mlabgap(1)), yscale(reverse range(`ymin' `ymax') nofextend) ylabel(none) ytitle("") xline(0, lpattern(dash) lcolor(gs8)) xtitle("Country random intercept, u_j (anocracies)") title("Anocracies") plotregion(margin(zero)) graphregion(margin(small)) legend(off)

graph save "Baseline - Willingness to Fight - Anocracies.gph", replace


/// Three above graphs merged into one

graph use "Baseline - Willingness to Fight - Autocracies.gph", name(g_auto, replace)
graph use "Baseline - Willingness to Fight - Anocracies.gph", name(g_ano, replace)
graph use "Baseline - Willingness to Fight - Democracies.gph", name(g_demo, replace)
graph combine g_auto g_ano g_demo, cols(3) title("Figure 2: Decomposed baseline willingness to fight by regime type")imargin(small) graphregion(color(white)) 

graph save "Figure 2 - Baseline - Willingness to Fight - Regime Types.gph", replace
graph export "Figure 2 - Baseline - Willingness to Fight - Regime Types.png", width(2400) replace

/// All countries
count if tag==1
local N = r(N)
local ymin = 0.5
local ymax = `N' + 0.5

egen order = rank(u_j) if tag==1, unique

twoway (rcap lo hi order if tag==1, horizontal lcolor(gs8) lwidth(medthin)) (scatter order u_j if tag==1, msymbol(o) mcolor(gs8) mlabel(COW_NUM) mlabcolor(black) mlabsize(vsmall) mlabpos(3) mlabgap(1)), yscale(reverse range(`ymin' `ymax') nofextend) ylabel(none) ytitle("") xline(0, lpattern(dash) lcolor(gs8)) xtitle("Country random intercept, u_j (all countries)") title("Figure 2: Baseline for all countries") plotregion(margin(zero)) graphregion(margin(small)) legend(off)
graph display , ysize(10) xsize(6)

graph save "Baseline - Willingness to Fight - All Countries.gph", replace
graph export "Baseline - Willingness to Fight - All Countries.png", width(2400) replace


/// Graphs

global country "lngdppc_lag_bar lngdppc_lag_within Polity2_lag_bar Polity2_lag_within"    
global individual "Male Married Age Age_sq Income Education_level" 
global opt "i.WVS_wave i.Outlier"      
meqrlogit Fight $country $opt $individual if touse==1, || ccodecow:, 

preserve

margins, at(Polity2_lag_within = (-5(2)5)) atmeans predict(mu fixedonly)
marginsplot, title("Predicted Pr(Willingness to Fight) by Within-Country Polity") ytitle("Predicted probability") xtitle("Within-country Polity (deviation)")

margins, at(Polity2_lag_bar = (-10(2)10)) atmeans predict(mu fixedonly)
marginsplot, title("Predicted Pr(Willingness to Fight) by Between-Country Polity") ytitle("Predicted probability") xtitle("Country mean Polity")

predict reffects, reffects
twoway (scatter reffects ccodecow, mlabel(ccodecow)), title("Country-Level Random Intercepts: Baseline Willingness to Fight") ytitle("Random Intercept (u_j)") xtitle("Country Code")

_pctile lngdppc_lag_bar, p(25 75)
local g25 = r(r1)
local g75 = r(r2)
margins, at(Polity2_lag_bar=(-10(5)10) lngdppc_lag_bar=(`g25' `g75')) predict(mu fixedonly)



///
global country "lngdppc_lag_bar lngdppc_lag_within Polity2_lag_bar Polity2_lag_within"    
global individual "Male Married Age Age_sq Income Education_level" 
global opt "i.WVS_wave i.Outlier"      
meqrlogit Fight $country $opt $individual if touse==1, || ccodecow:, 

* population-average AMEs
asdoc margins, dydx(Polity2_lag_bar Polity2_lag_within) predict(mu fixedonly)

///

global country "lngdppc_lag_bar lngdppc_lag_within Polity2_lag_bar Polity2_lag_within"    
global individual "Male Married Age Age_sq Income Education_level" 
global opt "i.WVS_wave i.Outlier"      
meqrlogit Fight $country $opt $individual if touse==1, || ccodecow:, 

* substantively meaningful contrasts (between)
asdoc margins, at(Polity2_lag_bar = (-10 10)) predict(mu fixedonly) post
lincom _b[2._at] - _b[1._at]

///

global country "lngdppc_lag_bar lngdppc_lag_within Polity2_lag_bar Polity2_lag_within"    
global individual "Male Married Age Age_sq Income Education_level" 
global opt "i.WVS_wave i.Outlier"      
meqrlogit Fight $country $opt $individual if touse==1, || ccodecow:, 

* within-country "shock" (e.g., +5 points)
asdoc margins, at(Polity2_lag_within = (0 5)) predict(mu fixedonly) post
lincom _b[2._at] - _b[1._at]


////

preserve
collapse (mean) fight_mean=Fight (mean) polity=Polity2_lag, by(ccodecow year)

bys ccodecow: egen x_centroid = mean(polity)
bys ccodecow: egen y_centroid = mean(fight_mean)
egen tag = tag(ccodecow)

* country names if needed
kountry ccodecow, from(cown)

* Below can be for the Research Note.
twoway (lpolyci fight_mean polity, degree(1) bwidth(1.5) level(95) fcolor(gs12%60) lcolor(gs12) lwidth(vthin)) (lpoly   fight_mean polity, degree(1) bwidth(2) lcolor(blue) lwidth(medthick)) (scatter y_centroid x_centroid if tag, msymbol(i) mlabel(NAMES_STD) mlabcolor(black) mlabsize(vsmall) mlabpos(0) mlabgap(1)), ytitle("Share 'Yes' (willingness to fight)") xtitle("Democracy (Polity2, -10 to +10)") ylabel(0(.1)1, angle(horizontal)) legend(off) title("Willingness to Fight and Democracy") note("Local polynomial (degree 1) smoother with bandwidth h=1.5 (Polity units)." "Results are robust to h in [1, 2]; see Appendix Fig. X.", size(small) span)
graph display , ysize(10) xsize(6)

* Below can be a robustness graph for the Appendix.
twoway (lpolyci fight_mean polity, degree(1) bwidth(1)  lcolor(blue*0.6) fcolor(blue%15)) (lpolyci fight_mean polity, degree(1) bwidth(2)  lcolor(red*0.6)  fcolor(red%10)), legend(order(1 "h=1" 2 "h=2")) ytitle("Share 'Yes'") xtitle("Polity") 



////Plots without Country Names

* 1) Create country mean of Fight and within-country deviation
bysort ccodecow: egen fight_bar = mean(Fight)
gen fight_within = Fight - fight_bar

* Tag one observation per country for plotting country means
bysort ccodecow: gen tag_country = _n == 1

* 2) Panel A: BETWEEN-country association (expect negative)
twoway (scatter fight_bar Polity2_lag_bar if tag_country, msymbol(o) msize(tiny) mcolor(gs8) jitter(2)) (lfit fight_bar Polity2_lag_bar if tag_country, lcolor(emerald) lwidth(medthick)), title("Between countries") ytitle("Average Willingness to Fight (Country-Level)") xtitle("Average Polity Score (Country-Level)") subtitle("Each point = one country average") legend(off) graphregion(color(white)) name(g_between, replace)

* 3) Panel B: WITHIN-country association (expect positive)
twoway (scatter fight_within Polity2_lag_within if tag_country, msymbol(o) msize(tiny) mcolor(gs8) jitter(2)) (lfit fight_within Polity2_lag_within if tag_country, lcolor(navy) lwidth(medthick)), title("Within countries") ytitle("Deviation from Country's Average Willingness to Fight") xtitle("Deviation from Country's Average Polity Score") yline(0, lpattern(dash) lcolor(gs10)) subtitle("Each point = country-level deviation") yline(0, lpattern(dash) lcolor(gs10)) legend(off) graphregion(color(white)) name(g_within, replace)

* 4) Combine into a single figure
graph combine g_between g_within, cols(2) title("Figure 3: Simpson's Paradox - Opposite signs between vs within") note("Left: Countries with higher average democracy have lower average willingness to fight.   " "Right: Within a country, when democracy increases above its average, willingness to fight also increases.  " "Markers are jittered; lines are linear fits (OLS).")

* Optional: export
graph save "Figure 3 - Simpsons Paradox.gph", replace
graph export "Figure 3 - Simpsons Paradox.png", width(2400) replace


////Plots with Country Names

* 1) Create country mean of Fight and within-country deviation
bysort ccodecow: egen fight_bar = mean(Fight)
gen fight_within = Fight - fight_bar

* Tag one observation per country for plotting country means
bysort ccodecow: gen tag_country = _n == 1

* 2) Panel A: BETWEEN-country association (expect negative)
twoway (scatter fight_bar Polity2_lag_bar if tag_country, msymbol(i) mlabel(COW_NUM) msize(tiny) mcolor(gs8) jitter(2)) (lfit fight_bar Polity2_lag_bar if tag_country, lcolor(emerald) lwidth(medthick)), title("Between countries: higher democracy  →  lower Fight") ytitle("Willingness to fight (country mean)") xtitle("Polity (country mean)") legend(off) graphregion(color(white)) name(g_between, replace)

* 3) Panel B: WITHIN-country association (expect positive)
twoway (scatter fight_within Polity2_lag_within if tag_country, msymbol(i) mlabel(COW_NUM) msize(tiny) mcolor(gs8) jitter(2)) (lfit fight_within Polity2_lag_within if tag_country, lcolor(navy) lwidth(medthick)), title("Within countries: ↑ democracy  →  ↑ Fight") ytitle("Willingness to fight (demeaned)") xtitle("Polity (within-country deviation)") yline(0, lpattern(dash) lcolor(gs10)) legend(off) graphregion(color(white)) name(g_within, replace)

* 4) Combine into a single figure
graph combine g_between g_within, cols(2) title("Simpson's Paradox: Opposite Signs Within vs Between") note("Markers are jittered; lines are OLS fits.")

* Optional: export
graph export "simpsons_paradox_fight_polity-Countrynames.png", width(2400) replace



////

* 1) Create country mean of Fight and within-country deviation
bysort ccodecow: egen fight_bar = mean(Fight)
gen fight_dev = Fight - fight_bar   // renamed from fight_within for clarity

* Tag one observation per country for plotting country means
bysort ccodecow: gen tag_country = _n == 1

* 2) Panel A: BETWEEN-country association (expected negative)
twoway (scatter fight_bar Polity2_lag_bar if tag_country, msymbol(o) msize(tiny) mcolor(gs8) jitter(2)) (lfit fight_bar Polity2_lag_bar if tag_country, lcolor(emerald) lwidth(medthick)), title("Between Countries: More Democracy → Lower Willingness to Fight") ytitle("Average Willingness to Fight (Country-Level)") xtitle("Average Polity Score (Country-Level)") subtitle("Each dot = one country average") legend(off) graphregion(color(white)) name(g_between, replace)

* 3) Panel B: WITHIN-country association (expected positive)
twoway (scatter fight_dev Polity2_lag_within if tag_country, msymbol(o) msize(tiny) mcolor(gs8) jitter(2)) lfit fight_dev Polity2_lag_within if tag_country, lcolor(navy) lwidth(medthick)), title("Within Countries: Increases in Democracy → Higher Willingness to Fight") ytitle("Deviation from Country's Average Willingness to Fight") xtitle("Deviation from Country's Average Polity Score") yline(0, lpattern(dash) lcolor(gs10)) subtitle("Each point = country-level deviation") legend(off) graphregion(color(white)) name(g_within, replace)

* 4) Combine into a single figure
graph combine g_between g_within, cols(2) title("Simpson's Paradox: Opposite Signs Between and Within Countries") note("Left: Countries with higher average democracy have lower average willingness to fight.   " "Right: Within a country, when democracy increases above its average, willingness to fight also increases.  " "Markers are jittered; lines are linear fits (OLS).")

* Optional export
graph export "simpsons_paradox_fight_polity_labeled.png", width(2400) replace


////

*Figure 4 for visualizing predicted probabilities 
meqrlogit Fight Polity2_lag_bar Polity2_lag_within lngdppc_lag_bar lngdppc_lag_within Male Married Age Age_sq Education_level i.WVS_wave i.Outlier || ccodecow:

* Average marginal effect of a 1-unit within Polity change
margins, dydx(Polity2_lag_within)

* AME for between (interpreted as cross-country mean difference)
margins, dydx(Polity2_lag_bar)

* Predicted probabilities at representative values (within-country effect)
margins, at(Polity2_lag_within=(-5(1)5))
marginsplot, recast(line) ciopts(lpattern(dash)) title("Within-country effect", position(12) ring(1)) subtitle("Deviation from country mean Polity (−5 to +5)", position(12) ring(0)) ytitle("Predicted probability of willingness to fight") xtitle("Deviation from country mean (Polity)") name(g_within, replace)
	
* And similarly for between-country effect
margins, at(Polity2_lag_bar=(-10(2)10))
marginsplot, recast(line) ciopts(lpattern(dash)) title("Between-country effect", position(12) ring(1)) subtitle("Average polity score (−10 to +10)", position(12) ring(0)) ytitle("Predicted probability of willingness to fight") xtitle("Average polity (Country mean)") name(g_between, replace)

* Combine both
graph combine g_between g_within, cols(2) title("Figure 4: Predicted probability of willingness to fight") subtitle("Between-country vs Within-country democracy effects") note("Lines show predicted probabilities from Mundlak model with 95% confidence intervals.") graphregion(color(white))

graph save "Figure 4 - Predicted Probability of Willingness to Fight.gph", replace
graph export "Figure 4 - Predicted Probability of Willingness to Fight.png", width(2400) replace


